package com.jjmmbb.stockdiary;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.app.DatePickerDialog;
import android.app.NotificationManager;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.TimePicker;

import com.jjmmbb.stockdiary.db.ItemManager;
import com.jjmmbb.stockdiary.ex.RepeatSetDialog;
import com.jjmmbb.stockdiary.pojo.Enums;
import com.jjmmbb.stockdiary.pojo.Item;

public class ItemEditActivity extends BaseActivity implements OnClickListener {
	private EditText titleView;
	private TextView titleView_decode, teamView, teamTitleView,
			timePlanCompleteView, timeAlarmDayView, timeAlarmTimeView,
			timeAlarmRepeatView, timeAlarmView_title;
	private Button btDoing, btFinish, btDelete, btOK, btShare, btCopy,
			btAutoLink, alarmView;

	// ImageView iconView;

	private String[] teamList;

	private boolean isEdit = true;
	private String goFromMain = "";

	int js = 1;
	boolean isJS = true;

	Item item;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.item_edit);

		// iconView = (ImageView) findViewById(R.id.item_icon);
		titleView = (EditText) findViewById(R.id.item_title);
		titleView_decode = (TextView) findViewById(R.id.item_title_decode);
		// desView = (EditText) findViewById(R.id.item_des);
		teamView = (TextView) findViewById(R.id.item_team);
		teamTitleView = (TextView) findViewById(R.id.item_team_title);
		timePlanCompleteView = (TextView) findViewById(R.id.item_timePlanComplete);
		timeAlarmDayView = (TextView) findViewById(R.id.item_timeAlarmDay);
		timeAlarmTimeView = (TextView) findViewById(R.id.item_timeAlarmTime);
		timeAlarmRepeatView = (TextView) findViewById(R.id.item_timeAlarmRepeat);
		timeAlarmView_title = (TextView) findViewById(R.id.item_timeAlarm_title);

		// bt_share = (Button) findViewById(R.id.item_more);
		btOK = (Button) findViewById(R.id.item_OK);
		btCopy = (Button) findViewById(R.id.item_copy);
		btAutoLink = (Button) findViewById(R.id.item_autolink);
		btShare = (Button) findViewById(R.id.item_share);

		btOK.setOnClickListener(this);
		btCopy.setOnClickListener(this);
		btAutoLink.setOnClickListener(this);
		btShare.setOnClickListener(this);

		teamView.setOnClickListener(this);
		timePlanCompleteView.setOnClickListener(this);
		timeAlarmDayView.setOnClickListener(this);
		timeAlarmTimeView.setOnClickListener(this);
		timeAlarmRepeatView.setOnClickListener(this);

		ini();
		// addAD();
	}

	// 不会更新的视图初始化
	private void ini() {
		// 读取配置文件,初始化Comm的参数
		Util.getPreferences(this);
		// 获取数据库管理
		if (null == ap.c.itemManager) {
			ap.c.itemManager = new ItemManager(this);
		}

		// 判断是从住界面跳转过来,还是从通知栏跳转过来
		Bundle bundle = getIntent().getExtras();
		if (null != bundle) {
			goFromMain = bundle.getString("goFromMain");
		}
		if (null != goFromMain && goFromMain.equals("main")) {
			if (null != ap.c.item) {
				item = ap.c.item;
			} else {
				this.finish();
				return;
			}
		} else {
			// 获取需要提醒的记事项
			int id = Util.getPreferenceAlarmId(this);
			item = ap.c.itemManager.getById(id);
			if (null == item) {
				gotoExistActivity(MainActivity.class);
				isJS = false;
				this.finish();
			} else {
			}
			// Long now = new Date().getTime();
			// String sql = "select * from " + ItemDao.tableName
			// + " where type=0 and timeAlarm>0 and timeAlarm<" + now
			// + " order by timeAlarm desc";
			// ArrayList<Item> list = ap.c.itemManager.getListAsSQL(sql);//
			// 获取需要提醒的记事项
			// if (list != null && list.size() > 0) {
			// item = list.get(0);
			// } else {
			// this.finish();
			// return;
			// }
		}

		if (item.type == Enums.Item_Doing) {
			alarmView = (Button) findViewById(R.id.item_option1);
			btFinish = (Button) findViewById(R.id.item_option2);

			alarmView.setText(R.string.itemEdit_alarm_on);
			btFinish.setText(R.string.itemEdit_finish);

			alarmView.setOnClickListener(this);
			btFinish.setOnClickListener(this);
			// iconView.setImageResource(R.drawable.icon_doing);
		} else {
			btDoing = (Button) findViewById(R.id.item_option1);
			btDelete = (Button) findViewById(R.id.item_option2);

			btDoing.setText(R.string.itemEdit_doing);
			btDelete.setText(R.string.itemEdit_delete);

			btDoing.setOnClickListener(this);
			btDelete.setOnClickListener(this);
			// iconView.setImageResource(R.drawable.icon_finish);
		}

		titleView.setText(item.title);
		titleView.setSelection(item.title.length());
		titleView_decode.setText(item.title);

		// 设置按钮字体颜色
		// switch (item.type) {
		// case Enums.Item_Doing:
		// bt_doing.setTextColor(res.getColor(R.color.dodgerblue));
		// // titleView.setTextColor(res.getColor(R.color.dodgerblue));
		// break;
		// case Enums.Item_Finish:
		// bt_finish.setTextColor(res.getColor(R.color.dodgerblue));
		// // titleView.setTextColor(res.getColor(R.color.sandybrown));
		// break;
		// case Enums.Item_Delete:
		// bt_delete.setTextColor(res.getColor(R.color.dodgerblue));
		// // titleView.setTextColor(res.getColor(R.color.darkcyan));
		// break;
		// }

		ini2();

		// 调用,计时器实现
		HandlerJS handlerJS = new HandlerJS();
		Message message = handlerJS.obtainMessage(1);
		handlerJS.sendMessageDelayed(message, 1000); // 发送message
	}

	// 需要随时更新的视图初始化
	private void ini2() {
		// 提醒时间
		if (item.type == Enums.Item_Doing && item.typeAlarm > Enums.Alarm_No) {
			timeAlarmDayView.setVisibility(View.VISIBLE);
			timeAlarmTimeView.setVisibility(View.VISIBLE);
			timeAlarmRepeatView.setVisibility(View.VISIBLE);
			timeAlarmView_title.setVisibility(View.VISIBLE);
			alarmView.setTextColor(res.getColor(R.color.textEdit));

			// alarmView.setText(res.getString(R.string.itemEdit_alarm_off));

			if (item.timeAlarm <= 0) {
				Long now = new Date().getTime();
				item.timeAlarm = now;
			}
			timeAlarmDayView.setText(Util.getFormateDate(item.timeAlarm));
			timeAlarmTimeView.setText(Util.getFormateHour(item.timeAlarm));
			if (item.typeAlarm == Enums.Alarm_Once) {
				timeAlarmRepeatView.setText(getStr(R.string.msg_once));
			} else if (item.typeAlarm == Enums.Alarm_Daily) {
				timeAlarmRepeatView.setText(getStr(R.string.msg_everyday));
			} else if (item.typeAlarm <= 0) {
				// 出现未知的类型
				item.typeAlarm = Enums.Alarm_No;
				timeAlarmDayView.setVisibility(View.GONE);
				timeAlarmTimeView.setVisibility(View.GONE);
				timeAlarmRepeatView.setVisibility(View.GONE);
				timeAlarmView_title.setVisibility(View.GONE);
				// alarmView.setText(res.getString(R.string.itemEdit_alarm_on));
			} else {
				ArrayList<Integer> list = Util
						.MarkDecodeByBinary(item.typeAlarm);
				String strNum = "";
				for (int i = list.size() - 1; i >= 0; i--) {
					strNum += list.get(i) + " ";
				}
				if ("" != strNum) {
					timeAlarmRepeatView.setText(strNum + "");
				} else {
					item.typeAlarm = -2;
					item.update(ap.c.itemManager);
					timeAlarmRepeatView.setText(getStr(R.string.msg_once));
				}
			}

			Util.startServer(this);
		} else {
			item.timeAlarm = (long) 0;
			// timeAlarmDayView.setText(getStr(R.string.msg_null));
			// alarmView.setText(res.getString(R.string.itemEdit_alarm_on));
			timeAlarmDayView.setVisibility(View.GONE);
			timeAlarmTimeView.setVisibility(View.GONE);
			timeAlarmRepeatView.setVisibility(View.GONE);
			timeAlarmView_title.setVisibility(View.GONE);
		}
		// 计划完成时间
		// if (item.timePlanComplete > 0) {
		// timePlanCompleteView.setText(Util
		// .getFormateDate(item.timePlanComplete));
		// } else {
		// timePlanCompleteView.setText(getStr(R.string.msg_null));
		// }

		// 分组
		if (Comm.hide_team) {
			teamView.setVisibility(View.GONE);
			teamTitleView.setVisibility(View.GONE);
		} else {
			teamView.setVisibility(View.VISIBLE);
			teamTitleView.setVisibility(View.VISIBLE);
			if (isNull(item.team)) {
				teamView.setText(R.string.msg_null);
			} else {
				teamView.setText(item.team);
			}
		}
	}

	@Override
	public void onClick(View v) {
		item.title = titleView.getText().toString();
		switch (v.getId()) {
		case R.id.item_OK:
			item.update(ap.c.itemManager);
			this.finish();
			break;
		case R.id.item_option1:
			if (item.type == Enums.Item_Doing) {
				addAlarm();
			} else {
				item.doing(ap.c.itemManager);
				this.finish();
			}
			break;
		case R.id.item_option2:
			if (item.type == Enums.Item_Doing) {
				item.finish(ap.c.itemManager);
				this.finish();
			} else {
				item.delete(ap.c.itemManager, ap.c.list);
				this.finish();
			}
			break;
		case R.id.item_share:
			share();
			break;
		case R.id.item_copy:
			copyToClip(item.title);
			break;
		case R.id.item_autolink:
			if (isEdit) {
				decodeTitle();
				btAutoLink.setText(R.string.itemEdit_edit_title);
			} else {
				editTitle();
				btAutoLink.setText(R.string.itemEdit_decode_title);
			}
			break;
		// case R.id.item_delete:
		// if (item.type == Enums.Item_Delete) {
		//
		// } else {
		// item.delete(ap.c.itemManager, ap.c.list);
		// this.finish();
		// }
		// break;
		// case R.id.item_more:
		// popWindowMoreShow();
		// break;
		case R.id.item_team:
			popWindowTeamShow();
			break;
		case R.id.item_timeAlarmTime:
			timeAlarmHourSelect();
			break;
		case R.id.item_timeAlarmDay:
			timeAlarmDaySelect();
			break;
		case R.id.item_timeAlarmRepeat:
			timeAlarmRepeatSelect();
			break;
		case R.id.item_timePlanComplete:
			timePlanCompleteSelect();
			break;
		}
	}

	/**
	 * 分享到微博等
	 */
	private void share() {
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain"); // 纯文本
		/*
		 * 图片分享 it.setType("image/png"); //添加图片 File f = new
		 * File(Environment.getExternalStorageDirectory() +"/Pictures/2.png");
		 * Uri u = Uri.fromFile(f); it.putExtra(Intent.EXTRA_STREAM, u);
		 */
		StringBuffer sb = new StringBuffer();
		String shareSign = res.getString(R.string.itemEdit_shareSign);
		sb.append(item.title).append("\n").append(shareSign);
		intent.putExtra(Intent.EXTRA_SUBJECT, "");
		intent.putExtra(Intent.EXTRA_TEXT, sb.toString());
		startActivity(Intent.createChooser(intent, getTitle()));
	}

	/**
	 * 解析文字,标记出网址和电话等
	 */
	private void decodeTitle() {
		isEdit = false;
		titleView.setVisibility(View.GONE);
		titleView_decode.setVisibility(View.VISIBLE);
	}

	/**
	 * 进入可编辑状态
	 */
	private void editTitle() {
		isEdit = true;
		titleView.setVisibility(View.VISIBLE);
		titleView_decode.setVisibility(View.GONE);
	}

	protected void onResume() {
		super.onResume();
		popWindowTeamIni();
		// 清楚通知栏消息
		final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		notificationManager.cancel(android.R.id.icon);
	};

	// 分组操作----------------------------------------------------------------
	private PopupWindow popWindowTeam;

	private TextView createPopItem(final String title) {
		final TextView shareView = new TextView(this);

		int dp8 = Util.dip2px(this, 8);

		shareView.setTextAppearance(this, R.style.MediumBlack);
		shareView.setText(title);
		shareView.setBackgroundResource(R.drawable.btn_b);
		shareView.setPadding(dp8, dp8, dp8, dp8);
		shareView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				popWindowTeamDismiss();
				item.team = title;
				item.update(ap.c.itemManager);
				teamView.setText(title);
			}
		});

		return shareView;
	}

	private void popWindowTeamIni() {
		teamList = Comm.team_list.split(",");

		LinearLayout popList = new LinearLayout(this);
		LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		popList.setOrientation(LinearLayout.VERTICAL);
		popList.setLayoutParams(lp);

		if (null == teamList || teamList.length == 0) {
		} else {
			for (String str : teamList) {
				if (isStr(str)) {
					popList.addView(createPopItem(str));
				}
			}
		}

		int dp8 = Util.dip2px(this, 8);
		final TextView manageView = new TextView(this);
		final TextView clearView = new TextView(this);

		manageView.setTextAppearance(this, R.style.MediumBlack);
		manageView.setText(res.getString(R.string.itemEdit_teamMange));
		manageView.setBackgroundResource(R.drawable.btn_b);
		manageView.setPadding(dp8, dp8, dp8, dp8);
		manageView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				popWindowTeamDismiss();
				gotoActivity(TeamEditActivity.class);
			}
		});

		clearView.setTextAppearance(this, R.style.MediumBlack);
		clearView.setText(res.getString(R.string.itemEdit_teamClear));
		clearView.setBackgroundResource(R.drawable.btn_b);
		clearView.setPadding(dp8, dp8, dp8, dp8);
		clearView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				popWindowTeamDismiss();
				item.team = "";
				item.update(ap.c.itemManager);
				teamView.setText(res.getString(R.string.itemEdit_team));
			}
		});
		popList.addView(clearView);
		popList.addView(manageView);

		popWindowTeam = null;// 先置空,再重新加载
		popWindowTeam = new PopupWindow(popList, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, true);

		popWindowTeam.setOutsideTouchable(true);
		popWindowTeam.setBackgroundDrawable(new BitmapDrawable());// 加了这句,才能使popWindow消失
	}

	private void popWindowTeamShow() {
		if (null != popWindowTeam) {
			popWindowTeam.showAsDropDown(teamView, 0, 0);
		}
	}

	private void popWindowTeamDismiss() {
		if (null != popWindowTeam) {
			popWindowTeam.dismiss();
		}
	}

	// 更多操作----------------------------------------------------------------
	private PopupWindow popWindowMore;

	private void popWindowMoreIni() {
		LinearLayout popList = new LinearLayout(this);
		LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		popList.setOrientation(LinearLayout.VERTICAL);
		popList.setLayoutParams(lp);

		int dp7 = Util.dip2px(this, 7);

		final TextView shareView = new TextView(this);
		shareView.setTextAppearance(this, R.style.MediumBlack);
		shareView.setTextColor(res.getColor(R.color.btnComm));
		shareView.setText(res.getString(R.string.itemEdit_share));
		shareView.setBackgroundResource(R.drawable.btn_b);
		shareView.setPadding(dp7, dp7, dp7, dp7);
		shareView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				share();
				popWindowMoreDismiss();
			}
		});

		final TextView copyView = new TextView(this);
		copyView.setTextAppearance(this, R.style.MediumBlack);
		copyView.setText(res.getString(R.string.itemEdit_copy));
		copyView.setBackgroundResource(R.drawable.btn_b);
		copyView.setPadding(dp7, dp7, dp7, dp7);
		copyView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				copyToClip(item.title);
				popWindowMoreDismiss();
			}
		});

		final TextView decodeView = new TextView(this);
		decodeView.setTextAppearance(this, R.style.MediumBlack);
		decodeView.setText(res.getString(R.string.itemEdit_decode_title));
		decodeView.setBackgroundResource(R.drawable.btn_b);
		decodeView.setPadding(dp7, dp7, dp7, dp7);
		decodeView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (isEdit) {
					decodeTitle();
					decodeView.setText(res
							.getString(R.string.itemEdit_edit_title));
				} else {
					editTitle();
					decodeView.setText(res
							.getString(R.string.itemEdit_decode_title));
				}
				popWindowMoreDismiss();
			}
		});

		popList.addView(decodeView);
		popList.addView(copyView);
		popList.addView(shareView);

		popWindowMore = new PopupWindow(popList, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, true);

		popWindowMore.setOutsideTouchable(true);
		popWindowMore.setBackgroundDrawable(new BitmapDrawable());// 加了这句,才能使popWindow消失
	}

	private void addAlarm() {
		if (item.typeAlarm > Enums.Alarm_No) {
			item.typeAlarm = Enums.Alarm_No;
			item.update(ap.c.itemManager);
			ini2();
		} else {
			Calendar iniTime = Calendar.getInstance();
			iniTime.add(Calendar.HOUR_OF_DAY, 1);
			item.timeAlarm = iniTime.getTimeInMillis();
			item.typeAlarm = Enums.Alarm_Once;
			item.update(ap.c.itemManager);
			ini2();// 刷新页面
		}
		// popWindowMoreDismiss();
	}

	private void popWindowMoreShow() {
		if (null == popWindowMore) {
			popWindowMoreIni();
		}
		popWindowMore.showAsDropDown(btShare, 0, 0);
	}

	public void popWindowMoreDismiss() {
		popWindowMore.dismiss();
	}

	// 复制的文本内容
	private void copyToClip(String strText) {
		android.text.ClipboardManager clipboardManager = (android.text.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
		clipboardManager.setText(strText);
		toast(R.string.msg_copySuccess);
	}

	// 完成时间设置
	private void timePlanCompleteSelect() {
		final Calendar iniTime = Calendar.getInstance();
		if (item.timePlanComplete > 0) {
			iniTime.setTimeInMillis(item.timePlanComplete);
		}
		DatePickerDialog dialog = new DatePickerDialog(this,
				new DatePickerDialog.OnDateSetListener() {

					public void onDateSet(DatePicker dp, int year, int month,
							int dayOfMonth) {
						iniTime.set(year, month, dayOfMonth);
						item.timePlanComplete = iniTime.getTimeInMillis();
						item.update(ap.c.itemManager);
						ini2();
					}
				}, iniTime.get(Calendar.YEAR), // 传入年份
				iniTime.get(Calendar.MONTH), // 传入月份
				iniTime.get(Calendar.DAY_OF_MONTH) // 传入天数
		);
		dialog.show();
	}

	// 提醒日期设置
	private void timeAlarmDaySelect() {
		final Calendar iniTime = Calendar.getInstance();
		if (item.timeAlarm > 0) {
			iniTime.setTimeInMillis(item.timeAlarm);
		}
		DatePickerDialog dialog = new DatePickerDialog(this,
				new DatePickerDialog.OnDateSetListener() {
					public void onDateSet(DatePicker dp, int year, int month,
							int dayOfMonth) {
						iniTime.set(year, month, dayOfMonth);
						item.timeAlarm = iniTime.getTimeInMillis();
						item.update(ap.c.itemManager);
						adjustTimeAlarm();
						ini2();
					}
				}, iniTime.get(Calendar.YEAR), // 传入年份
				iniTime.get(Calendar.MONTH), // 传入月份
				iniTime.get(Calendar.DAY_OF_MONTH) // 传入天数
		);
		dialog.show();
	}

	// 提醒时间设置
	private void timeAlarmHourSelect() {
		final Calendar iniTime = Calendar.getInstance();
		if (item.timeAlarm > 0) {
			iniTime.setTimeInMillis(item.timeAlarm);
		}
		TimePickerDialog dialog = new TimePickerDialog(this,
				new TimePickerDialog.OnTimeSetListener() {
					public void onTimeSet(TimePicker view, int hourOfDay,
							int minute) {
						// Calendar iniTime = Calendar.getInstance();
						iniTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
						iniTime.set(Calendar.MINUTE, minute);
						iniTime.set(Calendar.SECOND, 0);
						item.timeAlarm = iniTime.getTimeInMillis();
						adjustTimeAlarm();
						item.update(ap.c.itemManager);
						ini2();// 刷新布局
					}
				}, iniTime.get(Calendar.HOUR_OF_DAY),
				iniTime.get(Calendar.MINUTE), true);
		dialog.show();
	}

	// 设置提醒频率
	private void timeAlarmRepeatSelect() {
		RepeatSetDialog dialog = new RepeatSetDialog(this, item);
		dialog.show();
		dialog.setOnDismissListener(new OnDismissListener() {
			public void onDismiss(DialogInterface dialog) {
				adjustTimeAlarm();
				ItemEditActivity.this.ini2();
			}
		});
	}

	@Override
	public void finish() {
		if (null != ap.c.item && ap.c.item.id == item.id) {
			ap.c.item.title = item.title;
			ap.c.item.type = item.type;
			ap.c.item.typeAlarm = item.typeAlarm;
			ap.c.item.team = item.team;
			ap.c.item.time = item.time;
			ap.c.item.timePlanComplete = item.timePlanComplete;
			ap.c.item.timeFinish = item.timeFinish;
			ap.c.item.timeAlarm = item.timeAlarm;
		}
		if (null != goFromMain && goFromMain.equals("main")) {
		} else {
			gotoExistActivity(MainActivity.class);
		}
		isJS = false;
		super.finish();
	}

	public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			this.finish();
		}
		return super.onKeyDown(keyCode, event);
	};

	// 设置了提醒后,调整时间
	private void adjustTimeAlarm() {
		if (item.typeAlarm == Enums.Alarm_No) {
			return;
		}
		Calendar timeNow = Calendar.getInstance();
		Calendar timeAlarm = Calendar.getInstance();
		timeAlarm.setTimeInMillis(item.timeAlarm);

		if (timeNow.compareTo(timeAlarm) >= 0) {
			timeAlarm.add(Calendar.DAY_OF_MONTH, 1);
			item.timeAlarm = timeAlarm.getTimeInMillis();
			item.update(ap.c.itemManager);
		}

		// 到这里,提醒时间肯定比当前时间晚

		// 按星期提醒
		if (item.typeAlarm > 0) {
			ArrayList<Integer> listAlarm = Util
					.MarkDecodeByBinary(item.typeAlarm);
			if (listAlarm != null && listAlarm.size() > 0) {
				Calendar now = Calendar.getInstance();
				Calendar cal = Calendar.getInstance();
				cal.setTimeInMillis(item.timeAlarm);
				// 先将日期初始化到今天
				cal.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
						now.get(Calendar.DAY_OF_MONTH));

				for (int i = 0; i < 7; i++) {
					if (listAlarm.contains(getWeekOfDate(cal))) {
						item.timeAlarm = cal.getTimeInMillis();
						item.update(ap.c.itemManager);
						break;
					}
					cal.add(Calendar.DAY_OF_MONTH, 1);
				}
			}
		}
	}

	// 获取当前是星期几
	private int getWeekOfDate(Calendar cal) {
		int[] weekDays = { 7, 1, 2, 3, 4, 5, 6 };
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	// -----------------------------------计时保存---------------------
	class HandlerJS extends Handler {
		public void handleMessage(Message msg) { // handle message
			switch (msg.what) {
			case 1:
				// UI操作
				if (isJS) {
					js++;
					if (js % 10 == 0) {
						item.title = titleView.getText().toString();
						item.update(ap.c.itemManager);
						System.out.println(js);
					}
					// 发送message , 这样消息就能循环发送
					Message message = this.obtainMessage(1);
					this.sendMessageDelayed(message, 1000);
				}
			}
			super.handleMessage(msg);
		}
	};

	// -------------------添加广告---------------------------------------

	// RelativeLayout adContainer; // 广告
	//
	// void addAD() {
	// if (ap.c.isBuy) {
	// return;
	// }
	// // dolist: 56OJyHIYuMhuKSR9Qd
	// // anydolist: 56OJyHIYuNdLvklJ6y
	// // 56OJyM1ouMGoaSnvCK //测试
	// String PUBLISHER_ID = "56OJyHIYuNdLvklJ6y";
	//
	//
	// // 创建一个320x50的广告View
	// DomobAdView mAdview320x50;// 广告
	// mAdview320x50 = new DomobAdView(this, PUBLISHER_ID,
	// DomobAdView.INLINE_SIZE_320X50);
	// mAdview320x50.setKeyword("game");
	// mAdview320x50.setUserGender("male");
	// mAdview320x50.setUserBirthdayStr("2000-08-08");
	// mAdview320x50.setUserPostcode("123456");
	//
	// // 设置广告view的监听器。
	// mAdview320x50.setAdEventListener(new DomobAdEventListener() {
	// @Override
	// public void onDomobAdReturned(DomobAdView adView) {
	// }
	//
	// @Override
	// public void onDomobAdOverlayPresented(DomobAdView adView) {
	// }
	//
	// @Override
	// public void onDomobAdOverlayDismissed(DomobAdView adView) {
	// }
	//
	// @Override
	// public void onDomobAdClicked(DomobAdView arg0) {
	// }
	//
	// @Override
	// public void onDomobAdFailed(DomobAdView arg0, ErrorCode arg1) {
	// }
	//
	// @Override
	// public void onDomobLeaveApplication(DomobAdView arg0) {
	// }
	// });
	//
	// adContainer = new RelativeLayout(this);
	// // 将广告View增加到视图中。
	// adContainer.addView(mAdview320x50);
	//
	// // 布局
	// FrameLayout.LayoutParams lp_Top = new FrameLayout.LayoutParams(
	// FrameLayout.LayoutParams.MATCH_PARENT,
	// FrameLayout.LayoutParams.WRAP_CONTENT);
	// lp_Top.gravity = Gravity.BOTTOM;// 设置广告出现的位置(悬浮于底部)
	// addContentView(adContainer, lp_Top);
	//
	// // 检查广告更新
	// DomobUpdater.checkUpdate(this, PUBLISHER_ID);
	// }
}
